<?php
namespace App\Controllers;

use Phalcon\Mvc\Controller;
use App\Library\App;

class BaseController extends Controller {
    private $simpleView;
    private $memCache;

    public function initialize() {
    }

    /**
     * 前台业务，访问人数较多，不需要给全部用户都开启session，降低服务器的负载
     * 需要时，请在需要的Controller的initialize调用$this->startSession();
     */
    public function startSession() {
        if(!$this->session->isStarted()) {
            $this->session->start();
        }
    }

    /**
     * 检查用户是不是处于登录状态
     */
    public function checkLogin() {
        $uid = App::getCurrentUserUid();
        if($uid !== false) {
            return true;
        }
        if($this->request->isAjax()) {
            $this->sendAjax(false, '登录已经超时，请<a href="#/sign/in">点击</a>重新登录！');
        } else {
            echo '登录已经超时，请<a href="javascript:;" ng-click="$root.signout();">点击</a>重新登录；如果已重新登录（F5）或者<a href="javascript:;" ng-click="$root.reload();">点击</a>刷新浏览器！';
        }
        exit();
    }

    /**
     * 返回Phalcon\Mvc\View\Simple对象
     *
     * @return \Phalcon\Mvc\View\Simple
     */
    public function simpleView() {
        if(!is_object($this->simpleView)) {
            $this->simpleView = $this->getDI()->getShared('simple_view');
        }
        return $this->simpleView;
    }

    /**
     * 显示模板
     *
     * @param string $path
     * @param array  $params
     *
     * @return void
     */
    public function display($path = null, $params = null) {
        //Phalcon\Mvc\View
        $params = array_merge([], (array)$this->view->getParamsToView(), (array)$params);
        $this->view->setVars($params);
        if($path != null && $path != '') {
            $content = $this->simpleView()->render(trim($path, '/'), $params);
            $layout  = $this->view->getLayout();
            if($layout == '') {
                echo $content;
                exit();
            } else {
                $this->view->setContent($content);
            }
        }
        $this->view->render($this->dispatcher->getControllerName(), $this->dispatcher->getActionName(), $this->view->getParams());
    }

    /**
     * json数据输出
     * 如果data是array那么需要json_encode,如果字符串直接输出
     * $unescaped 是否非转义如果选择false,输出中文等多字节字符unicode;true,不编码
     *
     * @param array|string $data 需要发送的数据
     * @param bool         $unescaped 是否非转义 true非转义;false转义
     *
     * @return void
     */
    public function json($data, $unescaped = true) {
        $this->view->disable();
        header("Expires: 0");
        header("Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        header('Content-type: application/json;charset=utf-8');
        if(is_array($data)) {
            if($unescaped) {
                if($unescaped) {
                    echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK);
                } else {
                    echo json_encode($data, JSON_NUMERIC_CHECK);
                }
            } else {
                echo json_encode($data);
            }
        } else {
            echo $data;
        }
        return;
    }

    /**
     * 封装json输出
     *
     * @param bool  $status 发送的状态
     * @param mixed $data 发送的内容
     *
     * @return void
     */
    public function sendAjax($status, $data) {
        return $this->json([
            'status' => $status,
            'data'   => $data
        ]);
    }

    /**
     * 获取memcache对象
     *
     * @return \Phalcon\Cache\BackendInterface
     */
    public function memCache() {
        if($this->memCache == null) {
            $this->memCache = $this->getDI()->getShared('memCache');
        }
        return $this->memCache;
    }
}
